using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Framework.Library;
using Framework.Library.Components.Roles;
using Framework.Library.Components.Security;
using Framework.Library.Components.Users;
using Framework.Library.Utilities;
using Framework.Library.Components.Commands;
using Framework.Library.Components.Portal;

namespace Portal.Administrator.Roles
{
	public partial class RoleManagement : AuthenticatedControl
	{
		protected void Page_Load(object sender, EventArgs e)
		{
            if ((!CurrentUser.IsSuperAdmin && CommandController.IsSystemModule(AppEnv.CommandID())) || !CommandController.IsAvailableForUser(AppEnv.CommandID(), CurrentUser.UserID, Convert.ToInt32(AppEnv.PortalId())))
                Response.Redirect(AppEnv.ADMIN_CMD, true);
            if(!IsPostBack)
            {
                dropRoleParentID.Items.Clear();
                RoleController.LoadRoles(dropRoleParentID.Items);
                dropRoleParentID.Items.Insert(0, new ListItem("Root", "0"));
                
                lblCommandName.Text = AppEnv.CommandName();
            }
		}
		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);
		    
            DataTable dtRebuildData = new DataTable();

            dtRebuildData.Columns.Add("RoleID", typeof(String));
            dtRebuildData.Columns.Add("RoleName", typeof(String));
            dtRebuildData.Columns.Add("RoleDescription", typeof(String));
            dtRebuildData.Columns.Add("RoleParentID", typeof(String));
            dtRebuildData.Columns.Add("Level", typeof(String));

            DataTable dtRole;            
            dtRole = RoleController.GetRoles();
            dtgRoles.DataSource = RoleController.GetRoleByParentID(dtRole,dtRebuildData); 
            dtgRoles.ItemDataBound +=new RepeaterItemEventHandler(dtgRoles_ItemDataBound);
            dtgRoles.DataBind();
		  
		}
        protected void dtgRoles_ItemCommand(object sender, RepeaterCommandEventArgs e)
		{
			int roleId;
			switch (e.CommandName)
			{
				case "editrole":
					roleId = ConvertUtility.ToInt32(e.CommandArgument);
					RoleInfo role = RoleController.GetRole(roleId);
					txtRoleID.Text = role.RoleID.ToString();
					txtRoleName.Text = role.RoleName;
					txtRoleDescription.Text = role.RoleDescription;                 
                    dropRoleParentID.SelectedValue = role.RoleParentID.ToString();
					break;
				case "deleterole":
					try
					{
						roleId = ConvertUtility.ToInt32(e.CommandArgument);
						RoleController.DeleteRole(roleId);
                        lblUpdate.Text = MiscUtility.MSG_UPDATE_SUCCESS; 
					}
					catch (Exception ex)
					{
						Response.Write(ex.Message);
					}
					break;				
				default:
					break;
			}
		}
        protected void dtgRoles_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
            if (e.Item.ItemType == ListItemType.Item)
			{
                ImageButton btnDeleteRole = (ImageButton)e.Item.FindControl("btnDeleteRole");
                btnDeleteRole.Attributes.Add("onclick", MiscUtility.MSG_DELETE_CONFIRM);
                
			}
		}
		protected void btnUpdate_Click(object sender, EventArgs e)
		{
			try
			{
				int roleId = ConvertUtility.ToInt32(txtRoleID.Text);
				RoleInfo role = RoleController.GetRole(roleId);
				role.RoleName = txtRoleName.Text;
				role.RoleDescription = txtRoleDescription.Text;               
                role.RoleParentID = ConvertUtility.ToInt32(dropRoleParentID.SelectedValue);
				RoleController.UpdateRole(role);
                lblUpdate.Text = MiscUtility.MSG_UPDATE_SUCCESS; 
			}
			catch (Exception ex)
			{
				Response.Write(ex.Message);
			}
            btnCancel_Click(null, null);
		}
		protected void btnAdd_Click(object sender, EventArgs e)
		{
            if (txtRoleName.Text != "")
            {
                try
                {
                    RoleInfo role = new RoleInfo();
                    role.RoleName = txtRoleName.Text;
                    role.RoleDescription = txtRoleDescription.Text;
                    role.RoleParentID = ConvertUtility.ToInt32(dropRoleParentID.SelectedValue);
                    RoleController.AddRole(role);
                    lblUpdate.Text = MiscUtility.MSG_UPDATE_SUCCESS;
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                }
            }
            else
                lblUpdate.Text = MiscUtility.MSG_UPDATE_ERROR;
            btnCancel_Click(null, null);
		}		

        protected void btnCancel_Click(object sender, EventArgs e)
        {
           txtRoleName.Text = "";
           txtRoleDescription.Text = "";
           dropRoleParentID.SelectedIndex = -1;
        }

        protected void btnMember_Click(object sender, EventArgs e)
        {
            Response.Redirect(AppEnv.AdminUrlParams(CommandController.GetCommandbyID((int)PortalControls.UserRole)));           
        }

        protected void btnPermission_Click(object sender, EventArgs e)
        {
            Response.Redirect(AppEnv.AdminUrlParams(CommandController.GetCommandbyID((int)PortalControls.RolePermision)));
        }

	}
}